package cn.tedu._06artivle.controller;

import cn.tedu._06artivle.base.response.JsonResult;
import cn.tedu._06artivle.base.response.StatusCode;
import cn.tedu._06artivle.mapper.UserMapper;
import cn.tedu._06artivle.pojo.dto.UserArticleParam;
import cn.tedu._06artivle.pojo.dto.UserLoginParam;
import cn.tedu._06artivle.pojo.entity.Article;
import cn.tedu._06artivle.pojo.entity.User;
import cn.tedu._06artivle.pojo.vo.ArticleVo;
import cn.tedu._06artivle.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Validated
@RestController
@Tag(name = "用户模块")
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam={}", userLoginParam);

        UserVo userVo = userMapper.selectUser(userLoginParam);

        if(userVo == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVo);
        return JsonResult.ok(userVo);
    }

    @PostMapping("/article")
    @Operation(summary = "发布文章")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@RequestBody @Validated UserArticleParam userArticleParam, HttpSession session) {
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam, article);

        UserVo userVo = (UserVo) session.getAttribute("user");
        article.setUserId(userVo.getId());

        log.debug("userArticleParam={}", userArticleParam);
        int i = userMapper.insertArticle(article);
        if(i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("/selectByArticleId")
    @Operation(summary = "根据文章id查询文章详情")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByArticleId(@RequestParam @Range(min = 1, max = 100, message = "ID必须在1到100之间")
                                            Integer id) {
        Article article = userMapper.selectByArticleId(id);
        if(article == null) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

        User user = userMapper.selectUserById(article.getUserId());
        ArticleVo articleVo = new ArticleVo();

        articleVo.setContent(article.getContent());
        articleVo.setTitle(article.getTitle());
        articleVo.setNickName(user.getNickname());


        return JsonResult.ok(articleVo);
    }

}






